<template>

  <!--
      作者 luoyiming
      时间：2020-06-09
      描述：会员-等级管理-添加等级
  -->
  <el-dialog title="新增活動" v-model="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="600px">
    <el-form size="small" :model="form" ref="form">
      <el-form-item label="活動名稱" :label-width="formLabelWidth" prop="activeName" :rules="[{ required: true, message: ' ' }]">
        <el-input v-model="form.activeName" placeholder="請輸入活動名稱"></el-input>
      </el-form-item>
      <el-form-item label="滿類型" :label-width="formLabelWidth">
        <div>
          <el-radio v-model="form.fullType" :label="1">滿金額</el-radio>
          <el-radio v-model="form.fullType" :label="2">滿件數</el-radio>
        </div>
      </el-form-item>
      <el-form-item label="滿值" :label-width="formLabelWidth" prop="fullValue" :rules="[{ required: true, message: ' ' }]">
        <el-input type="number" :precision="1" :step="1" :min="0" placeholder="請輸入滿值" v-model="form.fullValue"></el-input>
      </el-form-item>
      <el-form-item label="減類型" :label-width="formLabelWidth">
        <div>
          <el-radio v-model="form.reduceType" :label="1">減金額</el-radio>
          <el-radio v-model="form.reduceType" :label="2">打折</el-radio>
        </div>
      </el-form-item>
      <el-form-item label="減值" :label-width="formLabelWidth" prop="reduceValue" :rules="[{ required: true, message: ' ' }]">
        <el-input type="number" :precision="1" :step="1" :min="0" placeholder="請輸入滿值" v-model="form.reduceValue"></el-input>
        <div class="tips">如果是打折，填寫30，表示減去30，打7折，訂單為原價的70%</div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible">取消</el-button>
        <el-button type="primary" @click="add()" :disabled="submit_loading">確定</el-button>
      </div>
    </template>
    
  </el-dialog>
</template>

<script>
import FullreduceApi from '@/api/fullreduce.js';
export default {
  data() {
    return {
      form: {
        /*活动名称*/
        activeName: '',
        /*满类型*/
        fullType: 1,
        /*满值*/
        fullValue: 0,
        /*减类型*/
        reduceType: 1,
        /*减值*/
        reduceValue: 0
      },
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      /*是否正在提交*/
      submit_loading: false
    };
  },
  props: ['open_add'],
  created() {
    this.dialogVisible = this.open_add;
  },
  methods: {
    /*添加等级*/
    add() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.submit_loading = true;
          FullreduceApi.add(params, true).then(data => {
            self.submit_loading = false;
            ElMessage({
              message: data.msg,
              type: 'success'
            });
            self.dialogFormVisible(true);
          }).catch(error => {
            self.submit_loading = false;
          });
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        });
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        });
      }
    }
  }
};
</script>

